#include "Engine.h"

// unique elements
FileSystem* FileSystem::fileSystem;
extern char workingFolderName[50];

// Init the file system.
FileSystem::FileSystem()
{  
    fileSystem = NULL;
}

// Delete the resource Path.
FileSystem::~FileSystem()
{
}

// Open a File.
FILE *FileSystem::Open(const char *file, const char *mode)
{
    char realPath[255];
#ifdef WIN32
	sprintf(realPath, "%s", file);
#else
    sprintf(realPath, "%s/%s", resourcePath, file);
#endif
    LOGE("\nRealPath:%s", realPath);
    return fopen(realPath, mode);
}


// Get the file Content 
char *FileSystem::GetContent(const char *file)
{

	//link with real path
	char realPath[100];

	strcpy( realPath, workingFolderName);
	strcat( realPath, file);

	FILE *f = Open(realPath, "rb");
	if(f==NULL)
	{
		return NULL;
	}
	else
	{
		fseek(f, 0, SEEK_END);
		int size = ftell(f);
		char *data = new char[size+1];
		fseek(f, 0, SEEK_SET);

		fread(data, size, 1, f);
		data[size] = 0;
		fclose(f);
		return data;
	}	
}

// Get the File system.
FileSystem *FileSystem::GetFileSystem()
{
	if ( fileSystem == NULL )
		fileSystem = new FileSystem();
    return fileSystem;
}
